package com.edu.controller;

import com.alibaba.fastjson.JSON;
import com.edu.Result;
import com.edu.StatusCode;
import com.edu.entity.JwtUtil;
import com.edu.pojo.EduRole;
import com.edu.pojo.EduTeacher;
import com.edu.service.EduRoleService;
import com.edu.service.EduTeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 管理员控制层
 *
 * @author YJF
 */
@RestController
@RequestMapping("/role")
public class EduRoleController {

    @Autowired
    private EduRoleService eduRoleService;

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询所有管理员
     */
    @GetMapping("/all")
    public Result findAll() {
        List<EduRole> roles = eduRoleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功!", roles);
    }

    /**
     * 登录用户
     */
    @PostMapping("/login")
    public Result login(@RequestBody EduTeacher eduTeacher, HttpServletResponse response) {
        EduTeacher teacher = eduTeacherService.findUser(eduTeacher);
        if (teacher == null) {
            return new Result(false, StatusCode.LOGIN_ERROR, "登录失败,用户名或密码错误!");
        }

        Map<String, Object> info = new HashMap<>();
        info.put("role", "Admin");
        info.put("success", "SUCCESS");
        info.put("tecName", teacher.getTecName());
        info.put("tecUser", teacher.getTecUser());
        // 1.生成令牌
        String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(info), null);
        // 2.设置cookie中
        Cookie cookie = new Cookie("Authorization", jwt);
        response.addCookie(cookie);
        // 3.设置头文件中
        response.setHeader("Authorization", jwt);
        List<String> result = new ArrayList<>();
        result.add(jwt);
        result.add(teacher.getTecName());
        return new Result(true, StatusCode.OK, "登录成功!", result);
    }

    /**
     * 分页条件查询用户的信息
     */
    @GetMapping("/list")
    public Result findByPage(HttpServletRequest request) {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String tecName = request.getParameter("tecName");
        String tecUser = request.getParameter("tecUser");
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setTecName(tecName);
        eduTeacher.setTecUser(tecUser);
        PageInfo<EduTeacher> teacherList = eduTeacherService.findRoles(page, size, eduTeacher);
        return new Result(true, StatusCode.OK, "查询成功!", teacherList);
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public Result addRole(@RequestBody EduTeacher eduTeacher) {
        int status = eduTeacherService.addUser(eduTeacher);
        Result result;
        switch (status) {
            case StatusCode.OK:
                result = new Result(true, StatusCode.OK, "添加成功!");
                break;
            case StatusCode.NOT_FIND:
                result = new Result(true, StatusCode.NOT_FIND, "未找到此教师!");
                break;
            case StatusCode.REP_ERROR:
                result = new Result(true, StatusCode.REP_ERROR, "重复操作!");
                break;
            default:
                result = new Result(true, StatusCode.REP_NAME, "用户名重复!");
        }
        return result;
    }

    /**
     * 根据用户ID查找用户信息
     */
    @GetMapping("/one/{id}")
    public Result getRole(@PathVariable("id") Integer id) {
        EduTeacher teacher = eduTeacherService.findRoleById(id);
        return new Result(true, StatusCode.OK, "查询成功!", teacher);
    }

    /**
     * 根据教师ID重置密码
     */
    @PutMapping("/reset/{id}")
    public Result rePwd(@PathVariable("id") Integer id) {
        int count = eduTeacherService.rePwd(id);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "密码更改成功!");
        }
        return new Result(false, StatusCode.ERROR, "修改失败!");
    }

    /**
     * 根据教师ID更新用户信息
     */
    @PutMapping("/update")
    public Result updateRole(@RequestBody EduTeacher eduTeacher) {
        eduTeacher.setUpdateTime(new Date());
        int count = eduTeacherService.updateRole(eduTeacher);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "修改成功!");
        }
        return new Result(false, StatusCode.ERROR, "修改失败!");
    }

    /**
     * 分配权限
     */
    @PutMapping("/power")
    public Result setPower(@RequestBody EduTeacher eduTeacher) {
        int count = eduTeacherService.setPower(eduTeacher);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "更新成功!");
        }
        return new Result(false, StatusCode.ERROR, "更新失败!");
    }

    /**
     * 删除权限用户
     */
    @PutMapping("/del")
    public Result delPower(@RequestBody EduTeacher eduTeacher) {
        int count = eduTeacherService.delPower(eduTeacher);
        if (count > 0) {
            return new Result(true, StatusCode.OK, "删除成功!");
        }
        return new Result(false, StatusCode.ERROR, "删除失败!");
    }
}
